package com.springboot.thymeleaf.springbootthymeleaf.filter;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @Classname ${小柯}
 * @Description TODO
 * @Date 2019/9/29 17:27
 * @Created by Administrator
 */

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Resource
    private MyTestInterceptor myTestInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 自定义拦截器，添加拦截路径和排除拦截路径
        registry.addInterceptor(myTestInterceptor).addPathPatterns("/login");
    }

    @Bean
    public FilterRegistrationBean testFilterRegistration(MyTestFilter myTestFilter) {
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(myTestFilter);//添加过滤器
        registrationBean.addUrlPatterns("/login");//添加过滤路径
        //registration.addInitParameter("name", "alue");//添加默认参数
        registrationBean.setName("MyFilter");
        registrationBean.setOrder(1);//设置优先级
        return registrationBean;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("js/**").addResourceLocations("classpath:/js/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


}
